﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using Angel.Entities;
using Angel.Daos.IManager;
using Angel.Daos.Manager;

namespace Angel.Web.Models
{
    public class AddModel
    {
        [Required(ErrorMessage = "Username field is required")]
        [CustomValidation(typeof(ValidationCustom),"ValidateDuplicateUsername")]
        [RegularExpression(@"^([^<]|a z|A Z|1 9|(.\.))*$", ErrorMessage ="Username is not contain '<' '>'")]
        [MinLength(6,ErrorMessage="The min length is 6 characters")]
        [MaxLength(20,ErrorMessage="The max length is 20 characters")]
        [Display(Name = "Username")]
        public string UserName { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("Password", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "Full Name field is required")]
        [Display(Name = "Full Name")]
        public String fullName { get; set; }

        [Required(ErrorMessage = "Email address field is required")]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Invalid format email address")]
        [Display(Name = "Email address")]
        public string Email { get; set; }
        [Display(Name = "Send Notification Email?")]
        public bool sendNotify { get; set; }
    }

    public class FilterModel
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
        public String userNameContain { get; set; }
        public String fullNameContain { get; set; }
        public String emailContain { get; set; }
        public int groupId { get; set; }
        public List<FilterUser> list { get; set; }
        public int count { get; set; }
    }

    public class EditUserModel
    {
        [Required]
        public int Id { get; set; }
        [Display(Name = "Full Name")]
        [MaxLength(20,ErrorMessage="FullName's Max length is 50 char")]
        [Required(ErrorMessage = "Full Name field is required")]
        public string FullName { get; set; }

        [Required(ErrorMessage = "Email field is required")]
        [DataType(DataType.EmailAddress)]
        [MaxLength(20, ErrorMessage = "Email's Max length is 50 char")]
        [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Invalid format email address")]
        [Display(Name = "Email address")]
        public string Email { get; set; }
    }

    public class DeleteUserModel
    {
        [Required]
        [Display(Name = "Id of username")]
        public long Id { get; set; }

        [Required]
        [Display(Name = "Username")]
        public string username { get; set; }
    }

    public class FilterUser
    {
        public int UserID { set; get; }
        public String UserName { get; set; }
        public String FullName { get; set; }
        public String Email { get; set; }
        public String LoginDetails { get; set; }
        public int GroupId{get;set;}
        public List<Group> Groups { get; set; }
        public String Directory { get; set; }
    }

    public class ValidationCustom
    {
        public static ValidationResult ValidateDuplicateUsername(String username)
        {
            bool isValid;
            IUserManager userManager = new UserManager();
            if (userManager.CheckUsername(username))
            {
                isValid = false;
            }
            else
            {
                isValid = true;
            }
            if (isValid)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult("Username already exists. Please enter a different username.");
            }
        }

        public static ValidationResult ValidateDuplicateEmail(String email)
        {
            bool isValid;
            IUserManager userManager = new UserManager();
            if (userManager.CheckEmail(email))
            {
                isValid = false;
            }
            else
            {
                isValid = true;
            }
            if (isValid)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult("Email already exists. Please enter a different email.");
            }
        }
    }
}